package com.eleven.mall.service.impl;

import com.eleven.mall.customexception.BusinessException;
import com.eleven.mall.mapper.CategoryMapper;
import com.eleven.mall.mapper.GoodsCatsMapper;
import com.eleven.mall.pojo.ExType;
import com.eleven.mall.pojo.GoodsCatsOut;
import com.eleven.mall.pojo.GoodsCatsPage;
import com.eleven.mall.pojo.entity.Category;
import com.eleven.mall.pojo.entity.GoodsCats;
import com.eleven.mall.pojo.entity.GoodsCatsExample;
import com.eleven.mall.service.GoodsCatsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class GoodsCatsServiceImpl implements GoodsCatsService {

    GoodsCatsMapper goodsCatsMapper;
    CategoryMapper categoryMapper;

    /**
     * 获取分类列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public GoodsCatsPage getGoodsCatsList(String query, Integer pagenum, Integer pagesize) {
        GoodsCatsPage goodsCatsPage = new GoodsCatsPage();
        GoodsCatsExample goodsCatsExample = new GoodsCatsExample();
        PageHelper.startPage(pagenum, pagesize);
        List<GoodsCats> goodsCatsList = goodsCatsMapper.selectByExample(goodsCatsExample);
        if (goodsCatsList == null || goodsCatsList.isEmpty()) {
            throw new BusinessException("goodsCatsList not found", ExType.BussRecordNotFound);
        }

        ArrayList<GoodsCatsOut> goodsCatsOuts = new ArrayList<>();
        for (GoodsCats gc : goodsCatsList) {
            GoodsCatsOut goodsCats = new GoodsCatsOut();
            goodsCats.setCat_id(gc.getCatId());
            goodsCats.setCat_name(gc.getCatName());
            goodsCats.setCat_level(getCatLevelById(gc.getCatId()));
            goodsCats.setCat_pid(gc.getParentId());
            goodsCats.setCat_deleted(getCatDeletedById(gc.getCatId()));
            goodsCatsOuts.add(goodsCats);
        }
        PageInfo<GoodsCats> pageInfo = new PageInfo<>(goodsCatsList);
        goodsCatsPage.setTotal((int)pageInfo.getTotal());
        goodsCatsPage.setPagenum(pageInfo.getPageNum());
        goodsCatsPage.setResult(goodsCatsOuts);

        return  goodsCatsPage;
    }

    //根据cat_id得到cat_level
    private Integer getCatLevelById(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey((int) id);
        if (category == null){
            throw new BusinessException("category not found", ExType.BussRecordNotFound);
        }
        return category.getCatLevel();
    }

    //根据cat_id 得到cat_deleted
    private Boolean getCatDeletedById(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey((int) id);
        if (category == null){
            throw new BusinessException("category not found", ExType.BussRecordNotFound);
        }
        //return category.getCatDeleted();
        int flag = category.getCatDeleted();
        if (flag==0){
            return false;
        }else if(flag == 1){
            return true;
        }
        return false;
    }


    /**
     * 添加分类
     * @param goodsCats
     * @return
     */
    @Override
    public Category addCategory(GoodsCats goodsCats) {
        return null;
    }

}
